package com.webcat.entity;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.webcat.enums.ResultEnum;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

/**
 * @author wangjun
 * @date 18-2-9 下午6:09
 * @description
 * @modified by
 */

@Data
@Slf4j
public class SseResponse<T> {
    private String chatId;

    private String event;

    private T data;

    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date serverTime;

    public SseResponse() {

    }

    public SseResponse(String chatId, String event, T data) {
        this.chatId = chatId;
        this.event = event;
        this.data = data;
    }

    public static<T> SseResponse<T> success(T data) {
        return success(null, data);
    }

    public static<T> SseResponse<T> success(String chatId, T data) {
        return success(chatId, null, data);
    }

    public static<T> SseResponse<T> success(String chatId, String event, T data) {
        SseResponse<T> response = new SseResponse<>(chatId, event, data);
        response.setServerTime(new Date());
        if (chatId != null && !chatId.isEmpty()) {
            response.setChatId(chatId);
        }
        log.info(JSONObject.toJSONString(response));
        return response;
    }

    public static<T> SseResponse<T> success() {
        SseResponse<T> response = new SseResponse<>();
        response.setServerTime(new Date());
        log.info(JSONObject.toJSONString(response));
        return response;
    }

    public static<T> SseResponse<T> fail(String code, String errMsg) {
        SseResponse<T> response = new SseResponse<>();
        response.setServerTime(new Date());
        log.info(JSONObject.toJSONString(response));
        return response;
    }
}
